import {Injectable} from '@angular/core';
import {CacheKey} from '../../shared/components/dhlcomponent/enums';
import {environment} from '../../../environments/environment';
import {appInjector} from '../../shared/utils/app-injector';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';
import {MessageService} from './message.service';
import {CacheDataService} from './cache.data.service';

@Injectable()
export class CommonService {


  protected apiBaseUrl = environment.apiBaseUrl;
  protected httpClient = appInjector().get(HttpClient);
  protected router = appInjector().get(Router);
  protected messageService = appInjector().get(MessageService);
  protected cacheService = appInjector().get(CacheDataService);


  public getRequestUrl(url) {
    if (!url) {
      return;
    }
    const projectCode = this.cacheService.getItem(CacheKey.ACTIVE_PROJECT);
    if (url.startsWith('/')) {
      url = `${this.apiBaseUrl}api${url}`;
    } else {
      if (projectCode) {
        url = `${this.apiBaseUrl}api/${projectCode.toLowerCase()}/${url}`;
      } else {
        const appCode = environment.appCode;
        url = `${this.apiBaseUrl}api/${appCode}/${url}`;
      }
    }
    return url;
  }

}
